package com.naizhuai.ui.management;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.CardView;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.Toast;

import com.naizhuai.R;
import com.naizhuai.network.exception.ApiException;
import com.naizhuai.network.exception.ServerException;
import com.naizhuai.ui.BaseActivity;
import com.naizhuai.ui.adapter.ManageDividerItemDecoration;
import com.naizhuai.ui.adapter.management.ManageOperateAdapterHolder;
import com.naizhuai.ui.components.SpinerPopWindow;
import com.naizhuai.ui.components.TipDialog;
import com.naizhuai.ui.entity.management.CompanyBean;
import com.naizhuai.ui.entity.management.OperateItemBean;
import com.naizhuai.Model.db.user.PersonUserBean;
import com.naizhuai.ui.entity.management.DepartmentBean;
import com.naizhuai.ui.management.presenter.CompanyManageDataApiStoreIml;
import com.naizhuai.util.AddPersonUtils;
import com.naizhuai.util.ConstantsPersonResources;
import com.naizhuai.util.ConstantsState;
import com.naizhuai.util.DialogUtils;
import com.naizhuai.util.IntentUtils;
import com.naizhuai.util.NaizhuAILog;
import com.naizhuai.util.SharedPreferenceUtil;
import com.naizhuai.util.ToastCompat;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import rx.Observer;

/**
 * 公司管理的主页
 */
public class CompanyManageMainActivity extends BaseActivity {
    private static final String TAG = "ActionMainActivity";


    @BindView(R.id.company_show_card)
    CardView operateCard;
    @BindView(R.id.company_operation_list)
    RecyclerView operateList;
    @BindView(R.id.scroll)
    ScrollView wrapScroller;
    @BindView(R.id.backImage)
    TextView loadText;
    //被邀请的人的id
    private String inviteId;

    //被邀请人的申请的权限
    //ConstantsState.COM_MANAGER_POWER_LEVEL 等
    private int powerType;

    private View dialogView;

    private List<OperateItemBean> operateLists;

    private List<DepartmentBean> teamsLists;
    private ManageOperateAdapterHolder adapter;
    private String[] addTeamOperate;
    private SpinerPopWindow<String> mSpinerPopWindow;
    private TipDialog loadingtipDialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        operateLists = new ArrayList<>();
        teamsLists = new ArrayList<>();
        initBasicData();
        initBasicView();
        requestDataFromService();
        InitDialog();
    }

    private void InitDialog() {
        if (loadingtipDialog == null) {
            loadingtipDialog = new TipDialog.Builder(this)
                    .setIconType(TipDialog.Builder.ICON_LOADING)
                    .setTipText("正在发送")
                    .create();
        }
    }

    private void initBasicView() {
        loadText.setVisibility(View.VISIBLE);
        loadText.setText(getText(R.string.is_loading));
        adapter = new ManageOperateAdapterHolder(this, operateLists, teamsLists);
        operateList.setHasFixedSize(true);
        operateList.setLayoutManager(new LinearLayoutManager(this));
        operateList.setItemAnimator(new DefaultItemAnimator());
        ManageDividerItemDecoration dividerDecoration = new ManageDividerItemDecoration(this, ManageDividerItemDecoration.VERTICAL_LIST, 5, true);
        dividerDecoration.setBaseFloat(0.3f);
        dividerDecoration.setDivideIndex(new int[]{operateLists.size() - 1});
        operateList.addItemDecoration(dividerDecoration);
        operateList.setAdapter(adapter);
        adapter.setOnItemClickListener(new ManageOperateAdapterHolder.OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position, int listIndex) {
                switch (listIndex) {
                    case -1:
                        addPersonToCompany(CompanyManageMainActivity.this);
                        break;
                    case 0:
                        switch (position) {
                            case 0:
                                Intent intentA = new Intent();
                                intentA.setClass(CompanyManageMainActivity.this, CompanyTotalStructureActivity.class);
                                CompanyManageMainActivity.this.startActivity(intentA);
                                break;
                            case 1:
                                IntentUtils.startActivityWithString(DepartmentBean.DEPARTMENT_ID_KEY, new String[]{getShareValue(CompanyBean.COMPANY_ID_KEY, ""), getShareValue(DepartmentBean.DEPARTMENT_ID_KEY, "")},
                                        CompanyManageMainActivity.this, CompanyTeamStructureActivity.class);
                                break;
                            case 2:
                                IntentUtils.startActivity(CompanyManageMainActivity.this, CompanyAddPartActivity.class);
                                break;
                        }
                        break;
                    case 1:
                        //任务列表--》点击进入任务详情界面


                        break;
                }
            }
        });
    }

    private void addPersonToCompany(Context mContext) {
        AddPersonUtils add = new AddPersonUtils(mContext, null, AddPersonUtils.ADD_TYPE_TO_COMPANY, new AddPersonUtils.AddPersonListener() {
            @Override
            public void onCompleted() {
            }

            @Override
            public void onError(Throwable e) {
                if (e instanceof ApiException) {
                    ApiException serverException = (ApiException) e;
                    ToastCompat.showToast(mContext, serverException.getDisplayMessage(), Toast.LENGTH_SHORT);
                }
                loadingtipDialog.dismiss();
            }

            @Override
            public void onNext(String integer) {
                loadingtipDialog.dismiss();
                if (ConstantsState.isResultOk(integer)) {
                    DialogUtils.showPromptDialog(mContext, getResources().getText(R.string.sendok).toString());
                } else {
                    DialogUtils.showPromptDialog(mContext, getResources().getText(R.string.sendfail).toString());
                }
            }
        });
        add.initView();
        loadingtipDialog.show();
    }

    private void requestDataFromService() {
        Observer<List<DepartmentBean>> observer = new Observer<List<DepartmentBean>>() {
            @Override
            public void onCompleted() {
            }

            @Override
            public void onError(Throwable e) {
                if (e instanceof ApiException) {
                    ApiException server = (ApiException) e;
                    DialogUtils.showPromptDialog(CompanyManageMainActivity.this, server.getDisplayMessage());
                } else {
                    initData();
                    setManageRecyclerViewTest(teamsLists);
                }
            }

            @Override
            public void onNext(List<DepartmentBean> result) {
                if (result == null)
                    result = new ArrayList<>();
                setManageRecyclerViewTest(result);
            }
        };

        CompanyManageDataApiStoreIml.getInstance().getTeamListByPerIds(
                SharedPreferenceUtil.getInstance(this).getValue(CompanyBean.COMPANY_ID_KEY, "1"))
                .subscribe(observer);

    }

    private void initBasicData() {

        setShareValue(CompanyBean.COMPANY_NAME_KEY, "东莞耐助智能有限公司");
        setShareValue(CompanyBean.COMPANY_ID_KEY, ConstantsPersonResources.COMPANY_ID_TEST_VALUE);
        setShareValue(CompanyBean.COMPANY_THUMB_URL_KEY, "https://ss1.bdstatic.com/70cFuXSh_Q1YnxGkpoWK1HF6hhy/it/u=2887997545,417602002&fm=27&gp=0.jpg");
        setShareValue(ConstantsPersonResources.SESSION_ID_KEY, ConstantsPersonResources.SESSION_TEST_VALUE);
        setShareValue(PersonUserBean.USER_ID_KEY, ConstantsPersonResources.USER_ID_TEST_VALUE);
        setShareValue(DepartmentBean.DEPARTMENT_ID_KEY, "1");
        NaizhuAILog.e(" setShareValue(PersonUserBean.USER_ID_KEY, ConstantsPersonResources.USER_ID_TEST_VALUE);" + setShareValue(PersonUserBean.USER_ID_KEY, ConstantsPersonResources.USER_ID_TEST_VALUE));

        String[] operateItemName = getResources().getStringArray(R.array.company_manage_items);
        operateLists.add(new OperateItemBean(getResources().getString(R.string.example_company_name), R.mipmap.company_logo_example, "", 0));
        operateLists.add(new OperateItemBean(operateItemName[0], ConstantsPersonResources.OPERATE_ITEM_RESOURCES_LISTS[0], "", 0));
        operateLists.add(new OperateItemBean(operateItemName[1], ConstantsPersonResources.OPERATE_ITEM_RESOURCES_LISTS[1], "", 0));
        operateLists.add(new OperateItemBean(operateItemName[2], ConstantsPersonResources.OPERATE_ITEM_RESOURCES_LISTS[2], getResources().getString(R.string.create_operate_details), R.drawable.ic_arrow_back_gray_24dp));

        addTeamOperate = getResources().getStringArray(R.array.company_power_level);
    }


    private void initData() {
        List<PersonUserBean> list = new ArrayList<>();
        list.add(new PersonUserBean("aa11", "han", "nickname1"));
        list.add(new PersonUserBean("aa12", "han1", "nickname11"));
        list.add(new PersonUserBean("aa13", "han2", "nickname12"));
        list.add(new PersonUserBean("aa14", "han3", "nickname13"));
        list.add(new PersonUserBean("aa15", "han4", "nickname14"));


        teamsLists.add(new DepartmentBean("aa11111", "第一小队", ConstantsPersonResources.TEST_TEAM_LOGO_URL[0], ConstantsState.TYPE_PRIVATE, list, null));
        list.add(new PersonUserBean("aa14", "han3", "nickname15"));
        list.add(new PersonUserBean("aa15", "han4", "nickname16"));
        teamsLists.add(new DepartmentBean("aa11111", "第二小队", ConstantsPersonResources.TEST_TEAM_LOGO_URL[1], ConstantsState.TYPE_PRIVATE, list, null));
        list.remove(2);
        teamsLists.add(new DepartmentBean("aa11111", "第三小队", ConstantsPersonResources.TEST_TEAM_LOGO_URL[2], ConstantsState.TYPE_PRIVATE, list, null));
        list.remove(3);
        teamsLists.add(new DepartmentBean("aa11111", "第四小队", ConstantsPersonResources.TEST_TEAM_LOGO_URL[3], ConstantsState.TYPE_PRIVATE, list, null));
        list.add(new PersonUserBean("aa14", "han3", "nickname13"));
        list.add(new PersonUserBean("aa15", "han4", "nickname14"));
        teamsLists.add(new DepartmentBean("aa11111", "第五小队", ConstantsPersonResources.TEST_TEAM_LOGO_URL[4], ConstantsState.TYPE_PRIVATE, list, null));
        list.remove(3);
        list.remove(1);
        teamsLists.add(new DepartmentBean("aa11111", "第六小队", ConstantsPersonResources.TEST_TEAM_LOGO_URL[5], ConstantsState.TYPE_PRIVATE, list, null));
        teamsLists.add(new DepartmentBean("aa11111", "第七小队", ConstantsPersonResources.TEST_TEAM_LOGO_URL[1], ConstantsState.TYPE_PRIVATE, list, null));
        teamsLists.add(new DepartmentBean("aa11111", "第八小队", ConstantsPersonResources.TEST_TEAM_LOGO_URL[3], ConstantsState.TYPE_PRIVATE, list, null));
        teamsLists.add(new DepartmentBean("aa11111", "第九小队", ConstantsPersonResources.TEST_TEAM_LOGO_URL[2], ConstantsState.TYPE_PRIVATE, list, null));
        teamsLists.add(new DepartmentBean("aa11111", "第十小队", ConstantsPersonResources.TEST_TEAM_LOGO_URL[5], ConstantsState.TYPE_PRIVATE, list, null));
        teamsLists.add(new DepartmentBean("aa11111", "第十一小队", ConstantsPersonResources.TEST_TEAM_LOGO_URL[4], ConstantsState.TYPE_PRIVATE, list, null));
        for (int i = 0; i < 20; i++) {
            teamsLists.add(new DepartmentBean("aa11111", "第十小队", ConstantsPersonResources.TEST_TEAM_LOGO_URL[5], ConstantsState.TYPE_PRIVATE, list, null));
            teamsLists.add(new DepartmentBean("aa11111", "第七小队", ConstantsPersonResources.TEST_TEAM_LOGO_URL[1], ConstantsState.TYPE_PRIVATE, list, null));
        }
    }


    private void setManageRecyclerViewTest(List<DepartmentBean> result) {
        teamsLists = result;
        if (result.size() == 0)
            loadText.setText(getText(R.string.has_no_message));
        else
            loadText.setVisibility(View.GONE);
        adapter.notifyDataSetChanged();
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_company_manage_main_layout;
    }


}
